package wxk.interview.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

/**
 * servlet是一些遵从java Servlet API的java类,这些Java类可以响应请求.
 * 尽管Servlet可以响应任意类型的请求，但是它们使用最广泛的响应web方面的请求。
 * Servlet必须部署在Java servlet容器才能使用。
 * JSP java servlet pages
 * jsf java servlet faces
 * 这些技术都要被servlet容器把页面翻译为java Servlet
 * <p>
 * Servlet生命周期的三个核心方法分别是 init() , service() 和 destroy()。
 * 每个Servlet都会实现这些方法，并且在特定的运行时间调用它们。
 *
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/6.
 * @since 2.2
 */
public class MySimpleServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("init config");
        super.init(config);
        Enumeration<String> enumeration = this.getServletConfig().getInitParameterNames();
        while (enumeration.hasMoreElements()){
            String name = enumeration.nextElement();
            String value = config.getInitParameter(name);
            System.out.println(name+":"+value);
        }
        System.out.println("context="+config.getServletContext().getInitParameterNames());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>MyFirstServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h2>Servlet MyFirstServlet at " + new Date() + req.getContextPath() + "</h2>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    public String getServletInfo() {
        return MySimpleServlet.class.getName();
    }

    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("init servlet ---> servlet config :"+this.getServletConfig());
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("destroy method invoked...");
    }

    private void downloadFile(HttpServletRequest request,HttpServletResponse response){
        String fileName = "logo.png";
        int bytes = 1024;
        int length = 0;
        try {
            ServletContext context = getServletConfig().getServletContext();
            String  mimeType = context.getMimeType(fileName);
            response.setContentType(mimeType != null ? mimeType : "text/plain");
            response.setHeader("Content-Disposition","attachment;fileName=\""+fileName+"\"");
            ServletOutputStream outputStream = response.getOutputStream();

            InputStream in = context.getResourceAsStream("/files"+fileName);

            byte[] buf = new byte[bytes];

            while (in != null && (length = in.read(buf)) != -1){
                outputStream.write(buf,0,length);
            }
            outputStream.flush();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
